Skip to content

ImageViewer

Base classes

Name Children Inherits
Widget
prettyqt.widgets.widget
The base class of all user interface objects.

⋔ Inheritance diagram

graph TD
  3317011476["custom_widgets.ImageViewer"]
  3664736470["widgets.Widget"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  3664736470 --> 3317011476
  829357528 --> 3664736470
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  3620195106 --> 3664736470
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: Widget

Source code in prettyqt/custom_widgets/imageviewer.py
class ImageViewer(widgets.Widget):
    def __init__(
        self,
        image: gui.QPixmap | os.PathLike[str] | str | None = None,
        *,
        show_border: bool = True,
        show_size: bool = False,
        border_width: int = 1,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self._border_width = border_width
        self._pixmap = gui.Pixmap()
        self.set_image(image)
        self.setMinimumSize(core.Size(150, 200))
        self.draw_border = show_border
        self.show_size = show_size

    def set_image(self, pixmap: gui.QPixmap | os.PathLike[str] | str | None):
        match pixmap:
            case gui.QPixmap():
                self._pixmap = pixmap
            case os.PathLike() | str():
                self._pixmap = gui.Pixmap.from_file(pixmap)
            case None:
                self._pixmap = gui.Pixmap()
        self.updateGeometry()
        self.update()

    def toggle_show_size(self):
        self.show_size ^= True
        self.update()

    def pixmap(self):
        return self._pixmap

    def sizeHint(self) -> core.QSize:
        return self.minimumSize() if self._pixmap.isNull() else self._pixmap.size()

    def paintEvent(self, event):
        super().paintEvent(event)
        pmap = self._pixmap
        w, h = pmap.width(), pmap.height()
        ow, oh = w, h
        cw, ch = self.rect().width(), self.rect().height()
        scaled, nw, nh = fit_image(w, h, cw, ch)
        if scaled:
            pmap = pmap.scaled(
                int(nw * pmap.devicePixelRatio()),
                int(nh * pmap.devicePixelRatio()),
                constants.AspectRatioMode.IgnoreAspectRatio,
                constants.TransformationMode.SmoothTransformation,
            )
        w = pmap.width() // pmap.devicePixelRatio()
        h = pmap.height() // pmap.devicePixelRatio()

        x = abs(cw - w) // 2
        y = abs(ch - h) // 2
        target = core.Rect(int(x), int(y), int(w), int(h))
        with gui.Painter(self) as p:
            p.setRenderHints(
                gui.Painter.RenderHint.Antialiasing
                | gui.Painter.RenderHint.SmoothPixmapTransform
            )
            p.drawPixmap(target, pmap)
            if self.draw_border:
                pen = gui.Pen()
                pen.setWidth(self._border_width)
                p.setPen(pen)
                p.drawRect(target)
            if self.show_size:
                draw_size(p, target, ow, oh)

:fa-brands-github: Show source on GitHub